package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.VersionDao;
import com.gdit.zax.entitys.Version;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;

public class CheckUpdate extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String version = request.getParameter("version");
		String system = request.getParameter("system");
		
		//检测参数
		if (TextUtil.isEmpty(version)) {
			O.printFail(response, 1, "请检测version参数!");
		}
		if (TextUtil.isEmpty(system)) {
			O.printFail(response, 1, "请检测system参数!");
		}
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		VersionDao vdao = dao.getVersionDao();
		Version lversion = vdao.hasUpdata(system, version);
		if (lversion==null) {
			O.printFail(response, 1, "已是最新版本!");
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("version", lversion.getVersion());
			map.put("url", Constant.WEBRoot +  lversion.getDownpath());
			O.printSuccess(response, map);			
		}
	}

}
